/**
 * 
 */
package handwritten.character;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Set;

import auth.User;

/**
 * @author Louis
 *
 */
public class CharacterCollectionManager {
	
	private static transient CharacterCollectionManager instance = null;
	
	private Hashtable<User,Hashtable<String,Hashtable<Class<? extends HandWritten>,CharacterCollection<?>>>> collections;
	
	
	
	
	public static CharacterCollectionManager getInstance(){
		if(instance == null)
			instance = new CharacterCollectionManager();
		return instance;
	}
	
	private CharacterCollectionManager(){
		collections = new Hashtable<User,Hashtable<String,Hashtable<Class<? extends HandWritten>,CharacterCollection<?>>>>();
	}
	
	public CharacterCollection<?> getCollection(User user,String collecType, Class<? extends HandWritten> type){
		if(collections.get(user) == null)
		{
			collections.put(user,new Hashtable<String,Hashtable<Class<? extends HandWritten>, CharacterCollection<?>>>());
		}
		if(collections.get(user).get(collecType)==null)
		{
			collections.get(user).put(collecType,  new Hashtable<Class<? extends HandWritten>,CharacterCollection<?>>());
		}
		if(collections.get(user).get(collecType).get(type) == null)
		{
			collections.get(user).get(collecType).put(type, CharacterCollectionFactory.getInstance().createCollection(type));
		}
		
		//System.out.println(collections.get(user).keySet().iterator().next());;
		return collections.get(user).get(collecType).get(type);
	}
	
	public ArrayList<CharacterCollection<?>> getCollections(User user){
		Set<Class<? extends HandWritten>> tCollection=null;
		Set<String> uCollections = collections.get(user).keySet();
		
		ArrayList<CharacterCollection<?>> result = new ArrayList<CharacterCollection<?>>();
		
		for(String sKey: uCollections)
		{
			tCollection= collections.get(user).get(sKey).keySet();
			
			for(Class<? extends HandWritten> cKey:tCollection)
				result.add(collections.get(user).get(sKey).get(cKey));
		}
		return result;
	}
	
	public Set<User> getUsers(){
		return collections.keySet();
	}
	
}
